package com.wsu.search.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
/**
 * Title:pacific topoyo.com
 * Description:配置文件读写类
 * Copyright: Copyright (c) 2009
 * Company:太平洋门户网络
 * 
 * @author 王谡 2009-10-15
 * @version 1.0
 */

public class GetProperties {
  //定义property对象
  Properties props = new Properties();
  //属性流
  InputStream is = null;
  public GetProperties(String fileStr) {
    //构造器初始化属性对象

    try {
      File file = new File(fileStr);
      is = new FileInputStream(file);
      props.load(is); //取文件列表
    }
    catch (Exception e) {

    }
  }

  /**
   * 取键值名称
   * @param keyName 键值名称
   * @return 键值
   */
  public String getKeyValue(String keyName) {
    return Tools.RmNull(props.getProperty(keyName));
  }
  /**
   * 设置属性值
   * @param keyName 属性名称
   * @param keyValue 属性值
   * @param comment 注释
   * @param filePath 文件路径 包含文件
   * @return
   */
public boolean setKeyValue(String keyName,String keyValue,String  comment,String filePath){
    try{
      OutputStream os = new FileOutputStream(new File(filePath));
      props.setProperty(keyName,keyValue);
      props.store(os,comment);
      os.close();
      os =null;
      return true;
    }
    catch(Exception e){
      System.out.println(e);
      return false;
    }
}
  /**
   * 关闭属性流和属性表
   */
  public void close() {
    try {
      is.close();
      is = null;
      props = null;
    }
    catch (IOException e) {
      System.out.println("Close Properties Err:" + e);
    }
  }
  public static void main(String[] args) {
    GetProperties getProps = new GetProperties("e:\\temp\\test.props");
    System.out.println(getProps.getKeyValue("username"));
    System.out.println(getProps.getKeyValue("passwd"));
    //getProps.setKeyValue("username","dddd","just a test","e:\\temp\\test.props");
    getProps.close();

  }

}
